package qldc.pm.tiensu.searchcontainer;

import java.text.SimpleDateFormat;

import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;

import qldc.pm.tiensu.dto.DoiTuongTienSuDTO;
import qldc.pm.tiensu.searchcontainer.util.nguoicotiensu.DoiTuongTienSuSearchUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class NguoiCoTienSuSearch extends SearchContainer<DoiTuongTienSuDTO> {
	private static Log _log = LogFactoryUtil.getLog(NguoiCoTienSuSearch.class);

	public static final String EMPTY_RESULTS_MESSAGE = "khong-tim-thay-doi-tuong-tien-su-nao";

	public NguoiCoTienSuSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
		super(renderRequest, new NguoiCoTienSuDisplayTerms(renderRequest),
				new NguoiCoTienSuSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
				DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		NguoiCoTienSuDisplayTerms displayTerms = (NguoiCoTienSuDisplayTerms) getDisplayTerms();

		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.SO_DINH_DANH_CA_NHAN, displayTerms.getSoDinhDanhCaNhan());
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.SO_CMND, displayTerms.getSoCMND());
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.SO_SO_HO_KHAU, displayTerms.getSoSoHokhau());
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.HO_VA_TEN, displayTerms.getHoVaTen());
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.NOI_THUONG_TRU_TINH_ID, String.valueOf(displayTerms.getNoiThuongTruTinhId()));
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.NOI_THUONG_TRU_QUAN_HUYEN_ID, String.valueOf(displayTerms.getNoiThuongTruQuanHuyenId()));
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.NOI_THUONG_TRU_PHUONG_XA_ID, String.valueOf(displayTerms.getNoiThuongTruPhuongXaId()));
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.NOI_THUONG_TRU_DIA_CHI, displayTerms.getNoiThuongTruDiaChi());
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.BAN_AN_SO, String.valueOf(displayTerms.getBanAnSo()));
		iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.TRANG_THAI,
				String.valueOf(displayTerms.getTrangThai()));
		if (displayTerms.getTuNgayPhamToi() != null) {
			iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.TU_NGAY,
					dateFormat.format(displayTerms.getTuNgayPhamToi()));
		}
		if (displayTerms.getNgayToaAn() != null) {
			iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.NGAY_TOA_AN,
					dateFormat.format(displayTerms.getNgayToaAn()));
		}

		if (displayTerms.getTuNgayToaAn() != null) {
			iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.TU_NGAY_TOA_AN,
					dateFormat.format(displayTerms.getTuNgayToaAn()));
		}
		if (displayTerms.getDenNgayToaAn() != null) {
			iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.DEN_NGAY_TOA_AN,
					dateFormat.format(displayTerms.getDenNgayToaAn()));
		}
		
		if (displayTerms.getDenNgayPhamToi() != null) {
			iteratorURL.setParameter(NguoiCoTienSuDisplayTerms.DEN_NGAY,
					dateFormat.format(displayTerms.getDenNgayPhamToi()));
		}
		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(renderRequest);

			String orderByCol = ParamUtil.getString(renderRequest, "orderByCol");
			String orderByType = ParamUtil.getString(renderRequest,"orderByType");
			String portletName = "tiensu";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			OrderByComparator orderByComparator = DoiTuongTienSuSearchUtil.getDoiTuongTienSuOrderByComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
			_log.warn(e);
		}
	}

}
